package cn.alise.controller.log;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ListUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.alise.controller.common.BaseController;
import cn.alise.qdp.model.common.Pagination;
import cn.alise.qdp.model.log.UserAccessLogs;
import cn.alise.qdp.service.log.UserAccessLogsService;


/**
 * 管理控制器
 */
@Controller
@RequestMapping(value = "/userAccessLogs")
public class UserAccessLogsController extends BaseController {

	private static final long serialVersionUID = 3071751434812887748L;

	@Resource(name = "userAccessLogsService")
	private UserAccessLogsService userAccessLogsService;

	/**
	 * 日志列表
	 */
	@RequestMapping(value = "/list")
	public String list(HttpServletRequest request, Model model,
			UserAccessLogs args, Pagination page) {
		
		HashMap<String, Object> params = new HashMap<String, Object>();
		
		// 查询日志记录总行数
		int total = this.userAccessLogsService.total(args);
		if (total > 0) {
			
			page.setTotalCount(total);
			params.put("pageSize",    String.valueOf(page.getRowsPerPage()));
			params.put("currentPage", String.valueOf(page.getCurrentPage()));
			
			//分页查询日志列表
			params.put("logId",     args.getLogId());
			params.put("userId",     args.getUserId());
			params.put("userName",     args.getUserName());
			params.put("roleName", args.getRoleName());
			params.put("account", args.getAccount());
			params.put("type", args.getType());
			params.put("path", args.getPath());
			
			// 设置分页信息
			params.put("start", (page.getCurrentPage() - 1) * page.getRowsPerPage());
			params.put("size",  page.getRowsPerPage());
			
			List<UserAccessLogs> list = this.userAccessLogsService.page(params);
			if (!CollectionUtils.isEmpty(list)) {
				model.addAttribute("list", list);
			} else {
				model.addAttribute("list", ListUtils.EMPTY_LIST);
			}
		}
		
		model.addAttribute("log", args);
		model.addAttribute("pagination", page);	
		
		return "pages/log/log-list";
	}
}